<?php
namespace app\admin\controller;

use app\common\model\UsedClassify as UsedClassifyModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 管理
 * Class Adminusedclassify
 * @package app\admin\controller
 *  默认(车型ID:1,品牌ID:2,车系ID:3,型号ID:4)
 */
class UsedClassify2 extends AdminBase
{
    protected $usedclassify_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->usedclassify_model = new UsedClassifyModel();

        // $usedclassify1_list = $this->usedclassify_model->where('pid',0)->select();
        $used_list = $this->usedclassify_model->select();
        $used_list_arr = $this->usedclassify_model->column('name','id');
        $this->assign('used_list', $used_list);
        $this->assign('used_list_arr', $used_list_arr);
    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($page = 1, $pid = [])
    {
        $usedclassify_level_list = $this->usedclassify_model->where('pid',0)->column('name','cid');

        if($pid){
            $js = $pid;
            $i = 0;
            for(;$i<count($pid);$i++){
                $app = array_pop($js);
                if($app != 0){
                    break;
                }
            }
            if($i == 0){
                $usedclassify_list = $this->usedclassify_model->where('id',$app)->where('pid','neq',0)->order('pid,sort,id DESC')->paginate(15, false, ['page' => $page]);
            }else{
                array_pop($pid);
                $xulie = implode(',',$pid);
                $usedclassify_list = $this->usedclassify_model->where('pid',$xulie)->whereOr('id',$app)->where('pid','neq',0)->order('pid,sort,id DESC')->paginate(15, false, ['page' => $page]);
            }
        }else{
            $usedclassify_list = $this->usedclassify_model->where('pid','neq',0)->order('pid,sort,id DESC')->paginate(15, false, ['page' => $page]);
        }

        return $this->fetch('index', ['usedclassify_list' => $usedclassify_list, 'usedclassify_level_list' => $usedclassify_level_list,'pid' => $pid]);
    }


    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'UsedClassify2');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {

                //设置默认选中车型  
                // dump($data['pid']);
                $i = 0;
                for(;$i<count($data['pid']);$i++){
                    if(!$data['pid'][$i]){
                        if($i == 0){
                            $first_id = $this->usedclassify_model->where('cid',1)->value('id');
                            $data['pid'] = $first_id;
                            break;
                        }else{
                            $data['pid'][$i] = 0;
                        }                    
                    }
                }

                if ($this->usedclassify_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $usedclassify = $this->usedclassify_model->where('pid','neq',0)->find($id);

        return $this->fetch('edit', ['usedclassify' => $usedclassify]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'UsedClassify2');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->usedclassify_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id = '', $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;

        if($ids){
            $i = 0;
            for(;$i<count($ids);$i++){
                $fileImg = $this->usedclassify_model->where('id',$ids[$i])->value('thumb');
                if($fileImg){
                   if(file_exists('./'.$fileImg)){
                       unlink('./'.$fileImg);
                   } 
                }
            }
        }
    
        if ($id) {
            if ($this->usedclassify_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }


    /**
     * 级联操作
     */
    public function jilm($id)
    {
        $jilm_select = $this->usedclassify_model->select();

        $i = 0;
        $bl = -1;
        $err = [];
        
        for(;$i<count($jilm_select);$i++){
            if( is_array($jilm_select[$i]['pid']) )
            {
                $arr_pid = $jilm_select[$i]['pid'];

                    $j = 0;
                    for(;$j<count($arr_pid);$j++){
                        if($j<2){
                            if($arr_pid[$j] == $id && $arr_pid[$j+1] == 0){
                                array_push($err,$jilm_select[$i]);
                                continue;
                            }
                        }else{
                            if($arr_pid[$j] == $id){
                                array_push($err,$jilm_select[$i]);
                                continue;
                            }
                        }
                        
                    }             
            }
            
        }
        $this->success('','',$err);

    }


}